package com.oauth.service.application.subscription.beans;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class MemberApplicationRelation {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long primaryKey;

	@Persistent
	private Long memberId;

	@Persistent
	private Date creationDate;

	@Persistent
	private Date lastUpdateDate;

	/**
	 * To keep track of all removed application.It is using list as it can
	 * contain duplicate values.
	 */
	@Persistent
	private List<Long> unSubscribedList;

	/**
	 * We are using set because application id's should be unique for given
	 * member.
	 */
	@Persistent
	private Set<Long> applicationIdSet;

	public MemberApplicationRelation(Long pMemberId, Set<Long> pApplicationIdSet) {
		super();
		memberId = pMemberId;
		applicationIdSet = pApplicationIdSet;
		creationDate = new Date();
	}

	public Long getMemberId() {
		return memberId;
	}

	public void setMemberId(Long pMemberId) {
		memberId = pMemberId;
	}

	public Long getPrimaryKey() {
		return primaryKey;
	}

	public void setPrimaryKey(Long pPrimaryKey) {
		primaryKey = pPrimaryKey;
	}

	public Set<Long> getApplicationIdSet() {
		return applicationIdSet;
	}

	public void setApplicationIdSet(Set<Long> pApplicationIdSet) {
		applicationIdSet = pApplicationIdSet;
	}

	public void addApplication(Long pApplicationId) {
		applicationIdSet.add(pApplicationId);
	}

	public void removeApplication(Long pApplicationId) {
		applicationIdSet.remove(pApplicationId);
		unSubscribedList.add(pApplicationId);
	}

	/**
	 * @return the creationDate
	 */
	public Date getCreationDate() {
		return creationDate;
	}

	/**
	 * @param pCreationDate
	 *            the creationDate to set
	 */
	public void setCreationDate(Date pCreationDate) {
		creationDate = pCreationDate;
	}

	/**
	 * @return the lastUpdateDate
	 */
	public Date getLastUpdateDate() {
		return lastUpdateDate;
	}

	/**
	 * @param pLastUpdateDate
	 *            the lastUpdateDate to set
	 */
	public void setLastUpdateDate(Date pLastUpdateDate) {
		lastUpdateDate = pLastUpdateDate;
	}

	/**
	 * @return the unSubscribedList
	 */
	public List<Long> getUnSubscribedList() {
		return unSubscribedList;
	}

	/**
	 * @param pUnSubscribedList
	 *            the unSubscribedList to set
	 */
	public void setUnSubscribedList(List<Long> pUnSubscribedList) {
		unSubscribedList = pUnSubscribedList;
	}

}
